package com.zhangye.test.api;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhangye.test.model.Admin;
import com.zhangye.test.model.AdminMoney;
import com.zhangye.test.model.EasyAr;
import com.zhangye.test.model.WatchRecord;
import com.zhangye.test.service.AdminMoneyService;
import com.zhangye.test.service.EasyArService;
import com.zhangye.test.service.MoneyTypeService;
import com.zhangye.test.service.SafeSessionService;
import com.zhangye.test.service.WatchRecordService;
import com.zhangye.test.util.PageInfo;


/**
 * 零钱明细
 * 作者： 张叶
 * 创建时间：2018年10月18日  下午3:41:18
 */
@Controller
@RequestMapping("api/adminMoney")
public class AdminMoneyApi {

	@Autowired
	private AdminMoneyService adminMoneyService;
	
	@Autowired
	private SafeSessionService safeSessionService;
	
	@Autowired
	private EasyArService easyArService;
	
	@Autowired
	private MoneyTypeService moneyTypeService;
	
	@Autowired
	private WatchRecordService watchRecordService;
	
	
	/**
	 * 看视频得积分
	 * 1，判断登录人，作品是否存在
	 * 2，判断watchRecord是否有记录，添加
	 * 3，判断当天获得分数
	 * 作者： 张叶
	 * 创建时间：2018年10月22日  下午5:45:33
	 */
	@ResponseBody
	@RequestMapping(value = "/getMoney", method = RequestMethod.POST)
	public ApiResult getMoney(HttpServletRequest request,Long easyArId,String type) {
		ApiResult result = new ApiResult();
		result.msg.status = 1;
		result.msg.desc = "获取失败";
		if(easyArId != null && !"".equals(easyArId)){
			Admin admin = safeSessionService.getCurrent(request);
			if(admin != null && !"".equals(admin)){
				EasyAr ea = easyArService.selectByPrimaryKey(easyArId);
				if(ea != null && !"".equals(ea)){
					String where = " and admin_id = " + admin.getId() + " and easy_ar_id = " + easyArId + " and TO_DAYS(created_at) = TO_DAYS(NOW())";
					List<WatchRecord> list = watchRecordService.selectByWhere(where);
					if(list.size() == 0){
						double i = adminMoneyService.updateMoney(admin, ea,type);
						if(i != 0){
							result.data.put("money", i);
							result.msg.status = 0;
							result.msg.desc = "获取成功";
						}
					}
				}
			}
		}
		return result;
	}
	

	/**
	 * 列表
	 * 作者： 张叶
	 * 创建时间：2018年10月18日  下午3:44:40
	 */
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public ApiResult list(HttpServletRequest request,Integer page, Integer limit) {
		ApiResult result = new ApiResult();
		Admin admin = safeSessionService.getCurrent(request);
		if(admin != null && !"".equals(admin)){
			PageInfo pageInfo = new PageInfo();
			if (pageInfo.getPage() < 2
					|| pageInfo.getPage() >= pageInfo.getTotalPage()) {
				pageInfo.setCount(0);
			}
			if (limit != null) {// 每页显示条数
				pageInfo.setPageSize(limit);
			}
			if (page != null) {// 当前页
				pageInfo.setPage(page);
			}
			String where = "1 = 1";
			where += " and admin_id = " + admin.getId();
			String order = " order by created_date desc";
			List<Map<String, Object>> list = adminMoneyService.selectByPage(pageInfo, where, order);
			if(list.size() > 0){
				result.data.put("adminMoney", list);
				result.data.put("pageInfo", pageInfo.getTotalPage());
				result.msg.status = 0;
				result.msg.desc = "获取成功";
			}else{
				result.msg.status = 1;
				result.msg.desc = "暂无数据";
			}
		}else{
			result.msg.status = 2;
			result.msg.desc = "当前登录已失效，请重新登录";
		}
		return result;
	}

	
	/**
	 * 添加
	 * 作者： 张叶
	 * 创建时间：2018年10月18日  下午3:46:41
	 */
	@ResponseBody
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public ApiResult insert(String type,Double money,HttpServletRequest request){
		ApiResult result = new ApiResult();
		Admin admin = safeSessionService.getCurrent(request);
		if(admin != null && !"".equals(admin)){
			if(type != null && !"".equals(type) &&
					money != null && !"".equals(money)){
				AdminMoney am = new AdminMoney();
				am.setAdminId(admin.getId());
				am.setType(type);
				am.setConsumeMoney(money);
				am.setConsumeDate(new Date());
				am.setCreatedDate(new Date());
				int i =  adminMoneyService.insert(am,admin);
				if(i > 0){
					result.msg.status = 0;
					result.msg.desc = "添加成功";
				}else{
					result.msg.status = 1;
					result.msg.desc = "系统错误，添加失败";
				}
			}
		}else{
			result.msg.status = 2;
			result.msg.desc = "当前登录已失效，请重新登录";
		}
		return result;
	}
	
}
